package com.adnaan.backend.controller.admin;

import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.SysRole;
import com.adnaan.backend.entity.vo.IdsVo;
import com.adnaan.backend.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * author: adnaan
 * create: 2025-01-11 15:19
 * Description:
 */
@Slf4j
@Tag(name = "角色表")
@RestControllerMapping("/sysRole")
@ApiSupport(author = "adnaan", order = 3)
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @PostMapping("/page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询")
    public Result<Page<SysRole>> pageQuery(@RequestBody PageParams<SysRole> pageParams) {
        // 1.查询参数
        SysRole params = pageParams.getParams();
        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.like(params.getName() != null, SysRole::getName, params.getName());
        lqw.like(params.getCode() != null, SysRole::getCode, params.getCode());
        lqw.eq(params.getStatus() != null, SysRole::getStatus, params.getStatus());
        // 3.分页查询
        return Result.success(sysRoleService.page(pageParams.getPage(), lqw));
    }

    @PostMapping("/insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增数据")
    public Result<Boolean> insert(@RequestBody SysRole sysRole) {
        if (sysRoleService.save(sysRole)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@RequestBody SysRole sysRole) {
        if (sysRoleService.updateById(sysRole)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除数据")
    public Result<Boolean> deleteById(@PathVariable String id) {
        if (sysRoleService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/batch")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "通过主键批量删除数据")
    public Result<Boolean> batchDelete(@RequestBody IdsVo ids) {
        if (sysRoleService.removeByIds(ids.getIds())) {
            return Result.success();
        }
        return Result.error();
    }

}
